package org.dxy.controller;

import org.dxy.entity.User;
import org.dxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

// 用户控制器
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/register")
    public String showRegisterPage() {
        return "register";
    }

    @PostMapping("/register")
    public String registerUser(@ModelAttribute User user, Model model) {
        try {
            User registeredUser = userService.register(user);
            model.addAttribute("message", "注册成功");
            return "login";
        } catch (Exception e) {
            model.addAttribute("error", "注册失败：" + e.getMessage());
            return "register";
        }
    }

    @GetMapping("/login")
    public String showLoginPage() {
        return "login";
    }

    @PostMapping("/login")
    public String loginUser(@RequestParam String usernameOrPhone, @RequestParam String password, Model model) {
        User user = userService.login(usernameOrPhone, password);
        if (user!= null) {
            // 将用户信息存入 session 或使用其他认证机制
            return "redirect:/home";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }

    // 其他用户相关的操作方法，如修改信息、重置密码等
}
